﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlSearchClass
{
    public class XmlSearchClass
    {
        /// <summary>
        /// this function will search in a document for attribute
        /// </summary>
        /// <param name="attributeName">attribute name</param>
        /// <param name="value">value</param>
        /// <param name="doc">document</param>
        /// <returns>return matched list</returns>
        public static List<XmlNode> searchByAttribute(string attributeName, string value, XmlDocument doc)
        {
            List<XmlNode> lstRet = searchByAttribute(attributeName, value, doc.ChildNodes);
            return lstRet;
        }


        /// <summary>
        /// this function will search in a list for attribute
        /// </summary>
        /// <param name="attributeName">attribute name</param>
        /// <param name="value">value</param>
        /// <param name="childList">list</param>
        /// <returns>return matched list</returns>
        public static List<XmlNode> searchByAttribute(string attributeName, string value,XmlNodeList childList)
        {
            List<XmlNode> lstRet = new List<XmlNode>();
            foreach (XmlNode xNode in childList)
            {
                if (xNode.Attributes != null)
                {
                    if (xNode.Attributes[attributeName] != null)
                    {
                        if (xNode.Attributes[attributeName].Value == value)
                        {
                            lstRet.Add(xNode);
                        }
                    }
                }


                lstRet.AddRange(searchByAttribute(attributeName,value,xNode.ChildNodes));
            }



            return lstRet;
        }
    





        /// <summary>
        /// this function will search in a document for a tagname
        /// </summary>
        /// <param name="tagName">tagname</param>
        /// <param name="doc">document</param>
        /// <returns>return matched list</returns>
        public static List<XmlNode> searchByTagName(string tagName, XmlDocument doc)
        {
            List<XmlNode> lstRet = searchByTagName(tagName, doc.ChildNodes);
            return lstRet;
        }


        /// <summary>
        /// this function will search in a List for a tagname
        /// </summary>
        /// <param name="tagName">tagname</param>
        /// <param name="childList">List</param>
        /// <returns>return matched list</returns>
        public static List<XmlNode> searchByTagName(string tagName, XmlNodeList childList)
        {
            List<XmlNode> lstRet = new List<XmlNode>();
            foreach (XmlNode xNode in childList)
            {
                if (xNode.Name != null)
                {
                    if (xNode.Name  == tagName)
                    {
                        lstRet.Add(xNode);
                    }
                }


                lstRet.AddRange(searchByTagName(tagName,xNode.ChildNodes));
            }



            return lstRet;
        }
    }
}
